<?php

/*

    MMetaverse - Open Metaverse Web Manager

    Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique

    http://www.maryjanesoft.com
    http://www.marlonj.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    Boston, MA  02111-1307  USA

    $Id$

*/

Yii::import('application.models.mmetaverse.MMetaverseModel');

/**
 * User identity based on first, lastname and password for OpenSim users 
 */
class MUserIdentity extends CUserIdentity {

    /**
     * User First Name
     * @var string
     */
    private $firstName;

    /**
     * User Last Name
     * @var string
     */
    private $lastName;

    /**
     * Create a user identity manager with the user info specified
     *
     * @param string $firstName User first name
     * @param string $lastName User last name
     * @param string $password User password
     */
    public function MUserIdentity($firstName, $lastName, $password) {
        // Construct the user identity with the username equals to first and lastname
        parent::__construct($firstName . ' ' . $lastName,$password);

        // Set names 
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }

    /**
     * Authenticates a user based on username and password.
     *
     * @return boolean whether authentication succeeds.
     */
    public function authenticate() {
        Yii::log('[Auth] Authenticate ' . $this->username,
            CLogger::LEVEL_INFO, 'application.MUserIdentity');

        // Get metaverse and authenticate user
        $metaverse = MMetaverseModel::getMetaverse();

        // Authenticate user
        $valid = $metaverse->authenticate(
            $this->firstName, $this->lastName, $this->password);

        // If valid set none error code
        if($valid) {
            $this->errorCode = CUserIdentity::ERROR_NONE;
            Yii::log('[Auth] Authenticate ' . $this->username . ' DONE',
                CLogger::LEVEL_INFO, 'application.MUserIdentity');
        }
        // else Unknow Identity
        else {
            $this->errorCode = CUserIdentity::ERROR_UNKNOWN_IDENTITY;
            Yii::log('[Auth] Authenticate ' . $this->username . ' FAIL',
                CLogger::LEVEL_INFO, 'application.MUserIdentity');
        }

        // Return boolean
        return !$this->errorCode;
    }
}

?>
